<?php
/**
 * Get Authenticated User Action Helper
 * When inside an action, one can use this helper to retrieve the currently
 * authenticated user.
 * 
 * @package Visi
 * @subpackage Action Helpers
 * @author Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version $Id$
 * @todo Make the view GetAuthUser use this helper so that user is not retrieved twice
 */
class Visi_Controller_Action_Helper_GetAuthUser extends Zend_Controller_Action_Helper_Abstract {

    protected $_user;
    
    public function direct() {
    
        if (is_null($this->_user)) {
            $isLoggedIn = Zend_Controller_Action_HelperBroker::getExistingHelper('IsLoggedIn');
            if ($isLoggedIn->direct()) {
                $user_id = Zend_Auth::getInstance()->getIdentity()->user_id;
                $modelCatalog = Zend_Controller_Action_HelperBroker::getExistingHelper('ModelCatalog');
                $select = $modelCatalog->direct('user')->select()->where("user_id = ?", $user_id);
                $this->_user = $modelCatalog->direct('user')->fetchRow($select);
            } else {
                throw new Visi_Controller_Action_Helper_Exception('There is no authenticated user.');
            }
        }
        return $this->_user;
    
    }

}